package com.web.bean;

import com.cache.impl.LxfCacheService;
import com.core.model.lxf.ShoppingCart;
import com.service.LxfService;
import com.web.cache.AbstractEntityCache;
import org.springframework.cache.CacheManager;
import org.springframework.cache.support.SimpleCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
import org.springframework.context.annotation.ScopedProxyMode;
import org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter;
import org.springframework.web.context.WebApplicationContext;

import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * User: lxf
 * Date: 2016/4/13
 * Time: 16:34
 * To change this template use File | Settings | File Templates.
 */
@Configuration
public class BeanManager {

    @Bean
    @Scope(value = WebApplicationContext.SCOPE_SESSION, proxyMode = ScopedProxyMode.TARGET_CLASS)
    public ShoppingCart shoppingCart() {
        return new ShoppingCart("衣服", "3520");
    }

    /**
     * 自定义CacheManager,开启基于内存的SimpleCacheManager
     * 可添加多个Cache
     *
     * @return
     */
    @Bean
    public CacheManager cacheManager() {
        SimpleCacheManager manager = new SimpleCacheManager();
        Collection<AbstractEntityCache> collection = new ArrayList<>();
        collection.add(new AbstractEntityCache("good"));
        collection.add(new AbstractEntityCache("abstractEntity"));
        manager.setCaches(collection);
        return manager;
    }


    @Bean
    public HttpInvokerServiceExporter httpInvokerServiceExporter(LxfService lxfService) {
        HttpInvokerServiceExporter exporter = new HttpInvokerServiceExporter();
        exporter.setService(lxfService);
        exporter.setServiceInterface(LxfService.class);
        return exporter;
    }

//    @Bean
//    public HandlerMapping httpInvokerMapping() {
//        SimpleUrlHandlerMapping mapping = new SimpleUrlHandlerMapping();
//        Properties mappings = new Properties();
//        mappings.setProperty("/spitter.service", "httpExportedSpitterService");
//        mapping.setMappings(mappings);
//        return mapping;
//    }


    @Bean
    public LxfCacheService lxfCacheService(LxfService lxfService) {
        LxfCacheService lxfCacheService = new LxfCacheService(lxfService);
        lxfCacheService.setExpireTimeInSecs(60 * 10); //缓存自动失效时间：10分钟
        return lxfCacheService;
    }

}
